﻿Public Class FrmDepositos
    Dim classController As New Class_RC_Controller.rc_Depositos
    Private _coddeposito As Integer = 0

    Public Property coddeposito() As Integer
        Get
            Return _codDeposito
        End Get
        Set(ByVal value As Integer)
            _coddeposito = value
        End Set
    End Property
    Private Sub FrmDepositos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarDatos()
    End Sub

    Private Sub CargarDatos()
        Try
            If coddeposito > 0 Then

                Dim ds As New DataTable
                ds = classController.get_Depositos_All(oiml.dbConn, coddeposito, "")

                If Not ds Is Nothing Then
                    txtcodigo.Text = ds.Rows(0)("codDeposito")
                    txtdescripcion.Text = ds.Rows(0)("descripcionDep")
                    txtUbicacion.Text = ds.Rows(0)("ubicacionDep")
                    txtobsDeposito.Text = ds.Rows(0)("obsDeposito")
                    btnEliminar.Enabled = True
                End If
            Else
                btnEliminar.Enabled = False
                txtdescripcion.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub
    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If
        If txtUbicacion.Text = "" Then
            ErrorProvider.SetError(txtUbicacion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtUbicacion, "")
        End If

        Return controlCampos

    End Function

    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If ValidarCampos() Then
            Exit Sub
        End If

        If coddeposito = 0 Then
            coddeposito = classController.Insert_Deposito(oiml.dbConn, txtdescripcion.Text, txtUbicacion.Text, txtobsDeposito.Text)
            If coddeposito > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_Deposito(oiml.dbConn, coddeposito, txtdescripcion.Text, txtUbicacion.Text, txtobsDeposito.Text) Then
                MessageBox.Show("Registro grabado con exito!", "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Depositos", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If
    End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub


    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If coddeposito > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Depositos", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_Deposito(oiml.dbConn, coddeposito) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Sector", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If
    End Sub

End Class